/**************************** (C) COPYRIGHT 2015 Fortiortech shenzhen *****************************
 * File Name          : IRScan.c
 * Author             : Billy Long Fortiortech  Market Dept
 * Version            : V1.0
 * Date               : 01/07/2015
 * Description        : This file contains main function used for Motor Control.
 ***************************************************************************************************
 * All Rights Reserved
 **************************************************************************************************/

#include <MyProject.h>
#include <FU68xx_4_MCU.h>

IRScan_TypeDef xdata IRScan;
IRControl_TypeDef xdata IRControl;
IRControl_AutoPowerDef AutoPowerState;

/*-------------------------------------------------------------------------------------------------
    Function Name : void IRInit(void)
    Description   :
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/

void IRInit(void)
{
    IRScan.BitValue = 0;
    IRScan.BitCnt = 0;
    IRScan.Bit0Cnt = 0;
    IRScan.UserCode = 0;
    IRScan.DataCode = 0;
    IRScan.IRReceiveFlag = 0;
    IRScan.CurrentUserCode = 0;
    IRScan.CurrentDataCode = 0;
    IRControl.FlagONOFF = 0;
    IRControl.FlagSpeed = 0;
    IRControl.TargetSpeed = 0;
    IRControl.ONOFFStatus = 0;
    IRControl.FlagAutoPower = 0;
    IRControl.FlagUserCodeSave = 0;
    IRControl.FlagUserCodeRead = 1; // 上电读出一次UserCode
    IRScan.OverFlowStatus = 0;
    mcFRState.OldTargetSpeed = 1;
    mcFRState.FR = CW;
    Time.PowerUpTimeCount = 0;
    AutoPowerState.FlagAutoPower = 0;
    AutoPowerState.Timer10sec = 0;
    AutoPowerState.ShutDowntime = 0;
    AutoPowerState.CurrentTime = 0;
#if (Motor_Speed_Control_Mode == SPEED_LOOP_CONTROL)
    {
        IRControl.SpeedLevel[0] = MOTOR_SPEED_LEVEL_0;
        IRControl.SpeedLevel[1] = MOTOR_SPEED_LEVEL_1;
        IRControl.SpeedLevel[2] = MOTOR_SPEED_LEVEL_2;
        IRControl.SpeedLevel[3] = MOTOR_SPEED_LEVEL_3;
        IRControl.SpeedLevel[4] = MOTOR_SPEED_LEVEL_4;
        IRControl.SpeedLevel[5] = MOTOR_SPEED_LEVEL_5;
        IRControl.SpeedLevel[6] = MOTOR_SPEED_LEVEL_6;
        IRControl.SpeedLevel[7] = MOTOR_SPEED_LEVEL_7; // MOTOR_FR_SPEED_LEVEL_7;
    }
#elif (Motor_Speed_Control_Mode == POWER_LOOP_CONTROL)
    {
        IRControl.SpeedLevel[0] = MOTOR_POWER_LEVEL_0;
        IRControl.SpeedLevel[1] = MOTOR_POWER_LEVEL_1;
        IRControl.SpeedLevel[2] = MOTOR_POWER_LEVEL_2;
        IRControl.SpeedLevel[3] = MOTOR_POWER_LEVEL_3;
        IRControl.SpeedLevel[4] = MOTOR_POWER_LEVEL_4;
        IRControl.SpeedLevel[5] = MOTOR_POWER_LEVEL_5;
        IRControl.SpeedLevel[6] = MOTOR_POWER_LEVEL_6;
    }
#endif
}

/*-------------------------------------------------------------------------------------------------
    Function Name : void SetSpeed(uint8 SpeedLevel)
    Description   : 速度设置
    Input         : SpeedLevel：速度挡位
    Output        : 无
-------------------------------------------------------------------------------------------------*/
void SetSpeed(uint8 SpeedLevel)
{
    IRControl.TargetSpeed = SpeedLevel;

    if (!mcSpeedRamp.FlagONOFF)
    {
        mcSpeedRamp.FlagONOFF = 1;
    }

    IRControl.ONOFFStatus = 1;
    IRControl.FlagSpeed = 1;
    mcFRState.OldTargetSpeed = IRControl.TargetSpeed;
}

/*-------------------------------------------------------------------------------------------------
    Function Name : void SetAutoPower(uint16 Time)
    Description   : 定时设置
    Input         : Time：定时时长
    Output        : 无
-------------------------------------------------------------------------------------------------*/
#if 0
void SetAutoPower(uint16 Time)
{
    IRControl.FlagAutoPower = 1;
    AutoPowerState.ShutDowntime = Time;
}
#endif
/*-------------------------------------------------------------------------------------------------
  Function Name : void RFValue(void)
  Description   : RF接收头解码
  Input         : 无
  Output              :   无
-------------------------------------------------------------------------------------------------*/
#if 0
void IRValue(void)
{
    static uint8 UserSavedStatus = 0;
    static idata uint32 TempByteValue = 0;
    static idata uint32 TempBitValue = 0;
    static uint8 UserSavedOneFlag = 0;
    uint8 tempof1 = 0;

    /* 读出高电平 */
    IRScan.Bit0Cnt = TIM3__DR;

    /* 找帧头->低电平长度6ms */
    if ((IRScan.Bit0Cnt <= TempLeadCodeMax) && (IRScan.Bit0Cnt >= TempLeadCodeMin))
    {
        IRScan.BitCnt = 1;
    }

    /* 表示找到帧头 */
    if (IRScan.BitCnt == 1)
    {
        IRScan.BitCnt = 2;
        TempByteValue = 0;
    }
    else if (IRScan.BitCnt >= 2)
    {
        IRScan.Bit0Cnt = TIM3__DR;
        /* 逻辑电平->1 */
        if ((IRScan.Bit0Cnt >= TempBitValue1Min) && (IRScan.Bit0Cnt <= TempBitValue1Max))
        {
            TempBitValue = 1;
            tempof1 = 0x00;
        }
        /* 逻辑电平->0 */
        else if ((IRScan.Bit0Cnt >= TempBitValue0Min) && (IRScan.Bit0Cnt <= TempBitValue0Max))
        {
            TempBitValue = 0;
            tempof1 = 0x00;
        }
        else
        {
            tempof1 = 0xff; // 当前数据电平长度不符合要求
        }

        if (tempof1 != 0xff)
        {
            /* 遥控解码部分 */
            TempByteValue += (TempBitValue << ((AllCodeLength - 1) - (IRScan.BitCnt - 2)));
            IRScan.BitCnt++;

            /* 接收满24位数据时进行处理(数据长度实际为25位，最后一位在起始信号时读取) */
            if ((IRScan.BitCnt - 2) == AllCodeLength)
            {
                IRScan.DataCode = (TempByteValue >> 0) & 0xFF;
                IRScan.UserCode = (TempByteValue >> 16) & 0XFFFF;
                TempByteValue = 0;

                /* 遥控接收处理部分 */
                /* 当前遥控地址码匹配则执行操作 */
                if ((IRScan.CurrentUserCode != 0) && (IRScan.UserCode == IRScan.CurrentUserCode))
                {
                    /* 一次接收数据中若数据相同只响应一次 */
                    if ((IRScan.CurrentDataCode != IRScan.DataCode) && (!IRScan.OverFlowStatus))
                    {
                        IRScan.CurrentDataCode = IRScan.DataCode;
                        IRScan.IRReceiveFlag = 1;
                        IRScan.OverFlowStatus = 1;
                    }
                }
                else
                {
                    /* 地址码为0或与当前地址不一致时执行读出地址码 */
                    IRControl.FlagUserCodeRead = 1;
                }
/***********遥控对码部分**************/
// 控制上电对码一次
#if (UserCodeStudyMode == LongPressMode)
                {
                    // 控制上电对码一次
                    if ((!UserSavedStatus) && (Time.PowerUpTimeCount < 60000) && (UserSavedOneFlag == 0))
                    {
                        // 长按OFF键对码
                        if (IRScan.DataCode == 0x00EF)
                        {
                            // 持续接收若干组码则完成匹配
                            {
                                IRControl.FlagUserCodeSave = 1;           // 执行存储
                                IRScan.CurrentUserCode = IRScan.UserCode; //
                                UserSavedStatus = 1;                      // 控制上电对码一次
                                UserSavedOneFlag = 1;
                            }
                        }
                    }
                }
#elif (UserCodeStudyMode == ShortPressMode)
                {
                    // 接收到遥控学习指令
                    if (IRScan.DataCode == StudyCode)
                    {
                        // 控制上电对码一次
                        if ((!UserSavedStatus) && (Time.PowerUpTimeCount < 60000))
                        {
                            IRScan.CurrentDataCode = IRScan.DataCode;
                            IRControl.FlagUserCodeSave = 1; // 执行存储
                            BZScan.FlagBZ = 1;
                            BZScan.BZMultitimes = 1; // 响三次
                            UserSavedStatus = 1;     // 控制上电对码一次
                        }
                    }
                }
#endif
            }
        }
        else
        {
            TempByteValue = 0;
            IRScan.BitCnt = 0;
        }
    }
}
#endif

/*-------------------------------------------------------------------------------------------------
    Function Name : void IRScanControl(void)
    Description   : 遥控信号处理
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/
#if 0
void IRScanControl(void)
{
    /* 档位 */
    uint8 position = 0;
    uint16 times_pos = 0;

    /* 接收到数据 */
    if (IRScan.IRReceiveFlag)
    {
        switch (IRScan.DataCode)
        {
        case IRONOFF:
        {
            if (mcSpeedRamp.FlagONOFF == 1)
            {
                IRControl.ONOFFStatus = 0;
                IRControl.TargetSpeed = 0;
                mcSpeedRamp.FlagONOFF = 0;

                // 在反转过程中关机时退出反转状态
                mcFRState.FR = CW;
                IRControl.FlagSpeed = 1;

                if (IRControl.LEDONOFFStatus == 0)
                {
                    SetLEDPin;
                    Set_LED(70, 0xF1); //
                }
                else
                {
                    ResetLEDPin;
                    Set_LED(550, 0xF1); // 0xF1
                }
            }
            else
            {
                IRControl.FlagAutoPower = 0;
                if ((mcFRState.OldTargetSpeed < 1) || (mcFRState.OldTargetSpeed > 7))
                {
                    mcFRState.OldTargetSpeed = 1;
                }
                SetSpeed(mcFRState.OldTargetSpeed);
                if (IRControl.LEDONOFFStatus == 0)
                {
                    SetLEDPin;
                }
                else
                {
                    ResetLEDPin;
                }
                Set_LED(300, mcFRState.OldTargetSpeed);
                MotorStatusSaveOnce = 1; // 存储档位信息
            }

            break;
        }

        case IRLED:
        {
            LEDPinONOFF;
            IRScan.IRReceiveFlag = 0;
            if (LEDPin == 1) // 读取LED 状态
                IRControl.LEDONOFFStatus = 0;
            else
                IRControl.LEDONOFFStatus = 1;
            MotorStatusSaveOnce = 1; // 存储状态信息
            break;
        }
        case IRSpeed1:
        case IRSpeed2:
        case IRSpeed3:
        case IRSpeed4:
        case IRSpeed5:
        case IRSpeed6:
        case IRSpeed7:
            if (IRScan.DataCode == IRSpeed1)
                position = 1;
            else if (IRScan.DataCode == IRSpeed2)
                position = 2;
            else if (IRScan.DataCode == IRSpeed3)
                position = 3;
            else if (IRScan.DataCode == IRSpeed4)
                position = 4;
            else if (IRScan.DataCode == IRSpeed5)
                position = 5;
            else if (IRScan.DataCode == IRSpeed6)
                position = 6;
            else if (IRScan.DataCode == IRSpeed7)
                position = 7;

            IRControl.FlagAutoPower = 0;
            SetSpeed(position);
            Set_LED(300, position);
            MotorStatusSaveOnce = 1; // 存储档位信息
            break;

        case IRAUTOPOWER1H:
        case IRAUTOPOWER2H:
        case IRAUTOPOWER4H:
        case IRAUTOPOWER6H:
        case IRAUTOPOWER8H:
            if (IRScan.DataCode == IRAUTOPOWER1H)
            {
                position = 1;
                times_pos = 360;
            }
            else if (IRScan.DataCode == IRAUTOPOWER2H)
            {
                position = 2;
                times_pos = 720;
            }
            else if (IRScan.DataCode == IRAUTOPOWER4H)
            {
                position = 4;
                times_pos = 1440;
            }
            else if (IRScan.DataCode == IRAUTOPOWER6H)
            {
                position = 6;
                times_pos = 2160;
            }
            else if (IRScan.DataCode == IRAUTOPOWER8H)
            {
                position = 8;
                times_pos = 2880;
            }
            SetAutoPower(times_pos);
            Set_LED(300, position);
            break;
        default:
            break;
        }

        IRScan.OverFlowStatus = 0;
        IRScan.IRReceiveFlag = 0;
    }
}
#endif

/*-------------------------------------------------------------------------------------------------
    Function Name : void AutoPowerControl(void)
    Description   : 自动控制开关机
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/
#if 0
void AutoPowerControl(void)
{
    if (IRControl.FlagAutoPower)
    {
        AutoPowerState.FlagAutoPower = 1;
        AutoPowerState.Timer10sec = 0; // 每次按遥控重新计时
        AutoPowerState.CurrentTime = 0;
        IRControl.FlagAutoPower = 0;
    }

    // 自动关机
    if ((AutoPowerState.FlagAutoPower && IRControl.ONOFFStatus))
    {
        if (++AutoPowerState.Timer10sec > 10000)
        {
            AutoPowerState.Timer10sec = 0;
            if (++AutoPowerState.CurrentTime >= AutoPowerState.ShutDowntime)
            {
                IRControl.ONOFFStatus = 0;
                mcFRState.OldTargetSpeed = 0;
                IRControl.TargetSpeed = 0;
                mcSpeedRamp.FlagONOFF = 0;
                IRControl.FlagSpeed = 1;
                AutoPowerState.CurrentTime = 0;
                AutoPowerState.FlagAutoPower = 0;
            }
        }
    }
    else
    {
        AutoPowerState.FlagAutoPower = 0;
        AutoPowerState.Timer10sec = 0;
        AutoPowerState.ShutDowntime = 0;
        AutoPowerState.CurrentTime = 0;
    }
}
#endif

/*-------------------------------------------------------------------------------------------------
    Function Name : void IRONOFF_Control(void)
    Description   : 射频开关机控制
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/
#if 0
void IRONOFF_Control(void)
{
    if ((mcSpeedRamp.TargetValue == 0) && (mcState != mcRun) && (mcState != mcStop))
    {
        /* 关机后堵转保护次数和缺相保护次数清零 */
        mcProtectTime.SecondStartTimes = 0;
        mcProtectTime.StallTimes = 0;
        mcProtectTime.LossPHTimes = 0;
        mcProtectTime.CurrentPretectTimes = 0;
    }
}
#endif
